package ua.kiev.tosik.price.gui;

import org.apache.log4j.Logger;
import ua.kiev.tosik.price.conf.AppConfig;
import ua.kiev.tosik.price.database.connectors.ConnectorFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

public class SplashWindow extends JWindow implements PropertyChangeListener {

	private ImageIcon splashImage = null;
	private String imageFile;
	private Logger logger = Logger.getLogger("ua.kiev.tosik.price.gui");
	private JPanel panel;
	private JLabel label;

	public SplashWindow() {
		super();
		logger.info("Starting splash screen window");
		// setAlwaysOnTop(true);
		splashImage = getSplashImage();

		final Dimension windowSize = (splashImage != null)?new Dimension(splashImage.getIconWidth(), splashImage.getIconHeight()):new Dimension(548, 366);
		logger.debug("Splash window: " + windowSize.getWidth() + "x" + windowSize.getHeight());

		this.setSize(windowSize);
		this.setLocationRelativeTo(null);

		label = new JLabel();
		label.setPreferredSize( new Dimension(300, 30) );
		label.setBounds(50, 100, 440, 30);
		label.setFont( label.getFont().deriveFont(16) );
		label.setFont( label.getFont().deriveFont(Font.BOLD) );
		label.setText("Loading...");

		panel = new JPanel() {
			protected void paintComponent(Graphics g) {
				if ( splashImage!= null ) {
					g.drawImage(splashImage.getImage(), 0, 0, null);
				} else {
					g.setColor(Color.white);
					g.fillRect(0, 0, (int)windowSize.getWidth(), (int)windowSize.getHeight());
					Graphics2D g2 = (Graphics2D)g;
					g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
					g2.setColor(Color.black);
					g2.setFont( label.getFont().deriveFont(Font.BOLD) );
					g2.drawString(AppConfig.getInstance().get("app.name"), 10, 20);
				}
				super.paintComponent(g);
			}
		};
		panel.setLayout(null);
		panel.setOpaque(false);
		panel.setPreferredSize(windowSize);
		panel.add(label);
		setContentPane(panel);

		try { com.sun.awt.AWTUtilities.setWindowOpaque(this, false); } catch (Exception e) { logger.warn("Can't set splash as transparent"); }

		panel.addMouseListener( new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if ( e.getClickCount() == 2 ) {
					destroy(true);
				}
			}
		});

	}

	public void propertyChange(PropertyChangeEvent evt) {
		if ( evt.getPropertyName().equals("Message") ) {
			label.setText("<html>" +  evt.getNewValue() + "</html>");
		}
	}

	private ImageIcon getSplashImage() {
		try {
			imageFile = AppConfig.getInstance().get("app.splashImage");
			if ( new File(imageFile).exists() ) {
				splashImage = new ImageIcon(imageFile);
				logger.debug("Load splash image [" + imageFile + " (" + splashImage.getIconWidth() + "x" + splashImage.getIconHeight() + ")] successfully");
			} else {
				logger.warn("Splash image not found!");
			}
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}
		return splashImage;
	}

	public void destroy() {
		super.setVisible(false);
		super.dispose();
		splashImage = null;
		logger.debug("Splash window closed");
	}

	private void destroy(boolean full) {
		try {
			ConnectorFactory.getInstance(AppConfig.getInstance().get("app.dbtype")).close();
		} catch (Exception e) {
			logger.warn(e.getLocalizedMessage());
		}
		super.setVisible(false);
		super.dispose();
		splashImage = null;
		logger.debug("Splash window closed");
		System.exit(0);
	}

}
